package Weapon

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionExchangeWeapon struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.ExchangeWeaponToC
	errcode cs_message.EnumType
}

func (this *transactionExchangeWeapon) GetModuleName() string {
	return "Weapon"
}

func (this *transactionExchangeWeapon) GetReplyMsg() proto.Message {
	return &cs_message.ExchangeWeaponToC{}
}

func (this *transactionExchangeWeapon) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionExchangeWeapon) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionExchangeWeapon) Begin() {

	this.resp = &cs_message.ExchangeWeaponToC{}

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), "ExchangeWeapon fail: role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call ExchangeWeapon", role.GetUserID(), role.GetID())

	weapon1 := role.GetRoleWeapon().GetWeaponByState(int32(1))
	weapon2 := role.GetRoleWeapon().GetWeaponByState(int32(2))

	//卸下武器
	if weapon1 != nil {
		role.GetRoleWeapon().DemountWeapon(weapon1.GetWeaponId())
		role.SetWeapon1(int32(0))
	}
	if weapon2 != nil {
		role.GetRoleWeapon().DemountWeapon(weapon2.GetWeaponId())
		role.SetWeapon2(int32(0))
	}

	//装备武器
	if weapon1 != nil {
		//原一号武器装备到二号位
		if !role.GetRoleWeapon().EquipWeapon(weapon1.GetWeaponId(), int32(2)) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "ExchangeWeapon fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}
		role.SetWeapon2(weapon1.GetWeaponId())
	}
	if weapon2 != nil {
		//原二号武器装备到一号位
		if !role.GetRoleWeapon().EquipWeapon(weapon2.GetWeaponId(), int32(1)) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "ExchangeWeapon fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}
		role.SetWeapon1(weapon2.GetWeaponId())
	}

	//重新计算战斗属性
	role.TotalBattleAttr()

	//将外观传到map
	role.ChangeOutline()

	//发送武器变化
	role.OnWeaponDirty()

	node_game.Infoln(role.GetUserID(), role.GetID(), "ExchangeWeapon OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionExchangeWeapon) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionExchangeWeapon) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.ExchangeWeaponToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionExchangeWeapon{
			user: user,
			req:  msg,
		}
	})
}
